package com.shire.shire_interface.controller.marketing;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.marketing.WxGroupKeyword;
import com.shire.shire_interface.model.marketing.WxGroupKeywordExample;
import com.shire.shire_interface.model.marketing.WxGroupKeywordItem;
import com.shire.shire_interface.model.marketing.WxGroupKeywordItemExample;
import com.shire.shire_interface.service.marketing.WxGroupKeywordItemService;
import com.shire.shire_interface.service.marketing.WxGroupKeywordService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//@Api(tags = "营销 关键词拉群")
@RestController
@RequestMapping("/groupKeyword")
public class WxGroupKeywordController extends BaseController {

    @Resource
    private WxGroupKeywordService wxGroupKeywordService;
    @Resource
    private WxGroupKeywordItemService wxGroupKeywordItemService;

//    @ApiOperation(value = "添加设置")
    @ResponseBody
    @PostMapping("/insert")
    public Response insert(@RequestBody WxGroupKeyword wxGroupKeyword, HttpSession session) {
        Response response = new Response();

        try {
            if (!checkLoginUserByCsId(session, wxGroupKeyword.getCsid())){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            if (StringUtils.isBlank(wxGroupKeyword.getKeyword())){
                response.error("请填写关键词!");
                return response;
            }

            wxGroupKeyword.setCreatedate(new Date());
            wxGroupKeywordService.inserBySelective(wxGroupKeyword);
            response.setData(wxGroupKeyword);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "修改设置")
    @ResponseBody
    @PostMapping("/update")
    public Response update(@RequestBody WxGroupKeyword wxGroupKeyword, HttpSession session){
        Response response = new Response();

        try {
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            wxGroupKeywordService.update(wxGroupKeyword);
            response.setData(wxGroupKeyword);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "get列表 csId, wxAccount")
    @ResponseBody
    @PostMapping("/get")
    public Response get(@RequestBody JSONObject jsonObject, HttpSession session) {
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            WxGroupKeywordExample wxGroupKeywordExample = new WxGroupKeywordExample();
            WxGroupKeywordExample.Criteria criteria = wxGroupKeywordExample.createCriteria().andWxaccountEqualTo(wxAccount);
            wxGroupKeywordExample.setOrderByClause("createDate desc");
            addPageParamToExample(jsonObject,wxGroupKeywordExample);
            List<WxGroupKeyword> wxGroupKeywords = wxGroupKeywordService.selectByExample(wxGroupKeywordExample);

            WxGroupKeywordItemExample wxGroupKeywordItemExample = new WxGroupKeywordItemExample();
            for (WxGroupKeyword wxGroupKeyword : wxGroupKeywords) {
                Integer id = wxGroupKeyword.getId();
                wxGroupKeywordItemExample.createCriteria().andCsidEqualTo(csId).andWxaccountEqualTo(wxAccount).andRespidEqualTo(id);

                List<WxGroupKeywordItem> wxGroupKeywordItems = wxGroupKeywordItemService.selectByExample(wxGroupKeywordItemExample);
                wxGroupKeyword.setWxGroupKeywordItemList(wxGroupKeywordItems);
            }
            response.setData(wxGroupKeywords);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "删除 id, csId, wxAccount")
    @ResponseBody
    @PostMapping("/del")
    public Response del(@RequestBody JSONObject jsonObject, HttpSession session) {
        Response response = new Response();
        try {
            Integer id = jsonObject.getInteger("id");
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            wxGroupKeywordService.delByPrimaryKey(id);

            WxGroupKeywordItemExample wxGroupKeywordItemExample = new WxGroupKeywordItemExample();
            wxGroupKeywordItemExample.createCriteria().andCsidEqualTo(csId).andRespidEqualTo(id).andWxaccountEqualTo(wxAccount);
            wxGroupKeywordItemService.delByExaple(wxGroupKeywordItemExample);

            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }


}
